import { param2Obj, apiData } from '@/utils'
import Mock from 'mockjs'
import LocalStorage from 'localStorage'
// 随机数方法
const getRandom = (min, max) => {
  min = min || 0
  max = max || 10
  return Math.floor(Math.random() * (max - min + 1) + min)
}

// 获取用户角色key
const getUserRolesKey = (id) => { return 'user_role_key_' + id }
// 获取用户部门key
const getUserDeptsKey = (id) => { return 'user_dept_key_' + id }

const Random = Mock.Random
const userListKey = 'user_getUserList'
var userList = JSON.parse(LocalStorage.getItem(userListKey))
if (userList === null || !userList) {
  userList = []
  const total = getRandom(50, 300)
  for (var i = 0; i < total; i++) {
    const item = Mock.mock({
      id: '@increment',
      username: '@name',
      nickname: '@cname',
      avatar() {
        return Random.image('200x200', Random.color(), '#000', this.username.substr(0, 3).trim())
      },
      email: '@email',
      'status|1': [0, 1],
      password: '@string(32)'
    })
    userList.push(item)
  }
  // 保存数据
  LocalStorage.setItem(userListKey, JSON.stringify(userList))
}

export default {
  getUserList: config => {
    const { searchKey, page = 1, limit = 20 } = param2Obj(config.url)
    let mockDatas = JSON.parse(LocalStorage.getItem(userListKey)) || []
    const search = searchKey ? searchKey.trim() : ''
    // 筛选
    if (search !== undefined && search !== '') {
      mockDatas = mockDatas.filter(item => {
        if (item.username.indexOf(search) < 0 && item.nickname.indexOf(search) < 0) {
          return false
        }
        return true
      })
    }

    // 处理当前数据
    const pageList = mockDatas.filter((item, index) => index < limit * page && index >= limit * (page - 1))

    return apiData('', '获取用户列表', { rows: pageList, total: mockDatas.length })
  },
  getUserRoles: config => {
    const { id } = param2Obj(config.url)
    const roles = JSON.parse(LocalStorage.getItem(getUserRolesKey(id))) || []
    return apiData('', '获取用户角色列表', roles)
  },
  saveUserRoles: config => {
    const { id, roles } = param2Obj(config.url)
    const userRoles = roles || []
    // 保存数据
    LocalStorage.setItem(getUserRolesKey(id), JSON.stringify(userRoles))
    console.log('xxxx', roles, config)
    return apiData('', '保存用户角色', userRoles)
  },
  getUserDepts: config => {
    const { id } = param2Obj(config.url)
    const depts = JSON.parse(LocalStorage.getItem(getUserDeptsKey(id))) || []
    return apiData('', '获取用户角色列表', depts)
  },
  saveUserDepts: config => {
    const { id, depts } = param2Obj(config.url)
    const userDepts = depts || []
    // 保存数据
    LocalStorage.setItem(getUserDeptsKey(id), JSON.stringify(userDepts))
    console.log('xxxx', depts, config)
    return apiData('', '保存用户角色', userDepts)
  },
  userDel: config => {
    const { id } = param2Obj(config.url)
    if (id <= 0) {
      return apiData(3001, '数据ID不能为空')
    }

    // 获取数据
    const allData = JSON.parse(LocalStorage.getItem(userListKey))
    const dVal = []
    for (var i = allData.length - 1; i >= 0; i--) {
      if (!(allData[i] !== null && allData[i]['id'] - id === 0)) {
        dVal.push(allData[i])
      }
    }
    // 保存数据
    LocalStorage.setItem(userListKey, JSON.stringify(dVal))

    return apiData('', '删除成功！')
  }
}
